package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {
    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysUserService userService;


    @GetMapping("/list")
    public TableDataInfo list(SysRole role)
    {
        startPage();
        List<SysRole> list = roleService.selectRoleList(role);

        return getDataTable(list);
    }
    @PostMapping
    public AjaxResult insert(@RequestBody SysRole sysRole){
        //少了逻辑判断 角色存在  以及  角色以及有的菜单权限
        sysRole.setCreateBy(getUsername());
        return toAjax(roleService.insertRole(sysRole));
    }
    @GetMapping("/{roleId}")
    public AjaxResult getByRoleId(@PathVariable Long roleId){
        SysRole sysRole = roleService.selectRoleById(roleId);
        return AjaxResult.success(sysRole);
    }
    @PutMapping("/dataScope")
    public  AjaxResult dataScope(@RequestBody SysRole sysRole){
        return AjaxResult.success(roleService.authDataScope(sysRole));
    }
    // 修改角色
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateRole(@RequestBody SysRole sysRole){
       return AjaxResult.success(roleService.updateRole(sysRole));
    }

    @DeleteMapping("{roleId}")
    public  AjaxResult delRole(@PathVariable Long[] roleId){
        return  AjaxResult.success(roleService.deleteRoleByIds(roleId));
    }
    // 查询角色已授权用户列表
    @GetMapping("/authUser/allocatedList")
    public  TableDataInfo allocatedUserList( SysUser sysUser){
        startPage();
        List<SysUser> sysUsers = userService.selectAllocatedList(sysUser);
        TableDataInfo rspData = new TableDataInfo();
        return getDataTable(sysUsers);
    }

    @GetMapping("/authUser/unallocatedList")
    public  TableDataInfo unallocatedList(SysUser sysUser){
        startPage();
        List<SysUser> sysUsers = userService.selectUnallocatedList(sysUser);
        return getDataTable(sysUsers);
    }
    @PutMapping("/authUser/selectAll")
    public  AjaxResult authUserSelectAll(Long roleId, Long [] userIds){
        userService.insertUserRoleAuth(roleId,userIds);
        return AjaxResult.success();
    }
    // 取消用户授权角色
    @PutMapping("/authUser/cancel")
    public AjaxResult authUserCancel(@RequestBody SysUser sysUser){
        userService.authUserCancel(sysUser);
        return AjaxResult.success();
    }

    @PutMapping("/authUser/cancelAll")
    public  AjaxResult authUserCancelAll(Long roleId,Long [] userIds){
        userService.authUserCancelAll(roleId,userIds);
        return AjaxResult.success();
    }
}